package com.anjubao.parking.municipal.order.controller;


import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.order.model.ParkingTradeOrder;
import com.anjubao.parking.municipal.order.service.ParkingTradeOrderService;
import com.anjubao.parking.municipal.payment.constant.OrderStatusEnum;
import com.anjubao.parking.municipal.payment.dto.*;
import com.anjubao.parking.municipal.payment.feign.ParkingTradeOrderRest;
import com.anjubao.parking.municipal.payment.form.BalanceRechargeForm;
import com.anjubao.parking.municipal.payment.form.OrderListQueryForm;
import com.anjubao.parking.municipal.payment.form.PayOrderForm;
import com.anjubao.parking.municipal.payment.form.SubmitOrderForm;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单controller
 *
 * @author zhuchunfeng
 */
@RestController
@RequestMapping("/parking-trade-orders")
@Slf4j
@Api(value = "Parkinglot-Order模块", tags = {"订单相关接口"})
public class ParkingTradeOrderController implements ParkingTradeOrderRest {

    @Resource
    private ParkingTradeOrderService parkingTradeOrderService;


    /**
     * 查询订单列表
     *
     * @param queryForm 查询条件
     * @return 订单列表
     */
    @ApiOperation("管理后台查询订单列表")
    @GetMapping("")
    public ApiResult<List<OrderListResponseDto>> queryParkingTradeOrderList(OrderListQueryForm queryForm) {
        IPage<OrderListResponseDto> parkingTradeOrderIPage = parkingTradeOrderService.queryParkingTradeOrderList(queryForm);
        return ApiResult.success(parkingTradeOrderIPage.getRecords(), (int) parkingTradeOrderIPage.getTotal());
    }

    /**
     * 获取订单状态枚举
     *
     * @return 订单状态枚举
     */
    @ApiOperation("获取订单状态枚举")
    @GetMapping("/order-status-enum")
    public ApiResult<List<EnumDescriptionDto>> listOrderStatus() {
        OrderStatusEnum[] values = OrderStatusEnum.values();
        List<EnumDescriptionDto> orderStatus = new ArrayList<>();
        for (OrderStatusEnum status : values) {
            orderStatus.add(new EnumDescriptionDto(status.getCode(), status.getDesc()));
        }
        return ApiResult.success(orderStatus);
    }

    /**
     * 生成订单并获取支付参数
     */
    @ApiOperation("提交订单")
    @PostMapping("/{systemType}/order-submit")
    public ApiResult<OrderResponseDto> submitOrder(@ApiParam(value = "1-开放式停车 2-封闭停车",name = "systemType") @PathVariable Integer systemType,
                                                   @RequestBody SubmitOrderForm submitOrderForm) {
        submitOrderForm.validate();
        return ApiResult.success(parkingTradeOrderService.submitOrder(submitOrderForm));
    }

    /**
     * 查询订单并获取支付参数
     */
    @ApiOperation("快捷支付")
    @PostMapping("/{systemType}/order-pay")
    public ApiResult<OrderResponseDto> quickPay(@ApiParam(value = "1-开放式停车 2-封闭停车",name = "systemType") @PathVariable Integer systemType,
                                                @RequestBody PayOrderForm payOrderForm){
        payOrderForm.validate();
        return ApiResult.success(parkingTradeOrderService.quickPay(payOrderForm));
    }

    /**
     * 直接获取支付参数
     */
    @ApiOperation("余额充值")
    @PostMapping("/balance-recharge")
    public ApiResult<PayResponseDto> balanceRecharge(@RequestBody BalanceRechargeForm balanceRechargeForm) {
        balanceRechargeForm.validate();
        return ApiResult.success(parkingTradeOrderService.balanceRecharge(balanceRechargeForm));
    }


    @ApiOperation("查询订单详情")
    @GetMapping("/{systemType}/order-detail")
    public ApiResult<ParkingTradeOrder> queryOrderDetail(@PathVariable Integer systemType,
                                                        @RequestParam(required = true) String businessId,
                                                         @RequestParam(required = true) Integer orderType) {
        return ApiResult.success(parkingTradeOrderService.queryOrderDetail(businessId,orderType));
    }

    @ApiOperation("用户端查询订单支付状态")
    @GetMapping("/{systemType}/order-status")
    public ApiResult<PayNotifyDto> queryOrderStatus(@PathVariable Integer systemType,
                                                    @RequestParam(required = true) String businessId,
                                                    @RequestParam(required = true) Integer orderType) {
        return ApiResult.success(parkingTradeOrderService.queryOrderStatus(businessId,orderType));
    }
}
